//
//  ZLWrongParsingCell.m
//  ZLExamination
//
//  Created by Zheng on 2017/9/13.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLWrongParsingCell.h"
#import "ZLWrongParsingQuestionTitleCell.h"
#import "ZLWrongParsingTableQuestionListCell.h"
#import "ZLWrongParsingDetailCell.h"
#import "ZLExamResultModel.h"

@interface ZLWrongParsingCell () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, copy) NSArray<NSString *> *questionTitles;


@end

@implementation ZLWrongParsingCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _questionTitles = @[@"A",@"B",@"C",@"D"];
        
        [self.contentView addSubview:self.myTableView];
        
        [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.bounces = NO;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.estimatedRowHeight = 100;
        [_myTableView registerClass:[ZLWrongParsingQuestionTitleCell class] forCellReuseIdentifier:kZLWrongParsingQuestionTitleCell_ID];
        [_myTableView registerClass:[ZLWrongParsingTableQuestionListCell class] forCellReuseIdentifier:kZLWrongParsingTableQuestionListCell_ID];
        [_myTableView registerClass:[ZLWrongParsingDetailCell class] forCellReuseIdentifier:kZLWrongParsingDetailCell_ID];
    }
    return _myTableView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 2) {
        return 1;
    } else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ZLWrongParsingQuestionTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLWrongParsingQuestionTitleCell_ID forIndexPath:indexPath];
        cell.questionLbl.text = _model.subject;
        return cell;
    } else if (indexPath.section == 1) {
        ZLWrongParsingTableQuestionListCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLWrongParsingTableQuestionListCell_ID forIndexPath:indexPath];
        
        cell.type = _model.optionTypes[indexPath.row];
        cell.model = _model;
        
        cell.optionsLbl.text = _questionTitles[indexPath.row];
        cell.questionLbl.text = _model.options[indexPath.row];
        
        return cell;
    } else {
        
        ZLWrongParsingDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLWrongParsingDetailCell_ID forIndexPath:indexPath];
        
        cell.contentLbl.text = _model.analysis;
        
        return cell;
    }

}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 40;
    } else {
        return 0.001;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        
        UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 40)];
        titleLbl.backgroundColor = kRGB_COLOR(242, 242, 242, 1);
        titleLbl.font = kMyFont(14);
        titleLbl.textAlignment = NSTextAlignmentCenter;
        titleLbl.textColor = [UIColor darkGrayColor];
        
        if ([_model.the_answer isEqualToString:@""] || [_model.the_answer isEqualToString:@" "]) {
            titleLbl.text = [NSString stringWithFormat:@"正确答案是 %@，你的答案是 未作答",_model.co_answer];
        } else {
            titleLbl.text = [NSString stringWithFormat:@"正确答案是 %@，你的答案是 %@",_model.co_answer,_model.the_answer];
        }
        
        return titleLbl;
    } else {
        return nil;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}



@end
